package com.studychen.appfxcp.commom.view;

import com.studychen.appfxcp.commom.model.BookkeepingListModel;
import com.studychen.appfxcp.commom.vo.BookkeepingItem;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.ResourceBundle;

public class BookkeepingListView implements FxmlView<BookkeepingListModel>, Initializable {


    private static final Logger log = LoggerFactory.getLogger(BookkeepingListView.class);
    @FXML
    private HBox topHBox;

    @FXML
    private Pagination page;

    @InjectViewModel
    private BookkeepingListModel bookkeepingListModel;



    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        log.info("view init");

        TableView<BookkeepingItem> tableView = new TableView<>();

        page.setPageFactory(new Callback<Integer, Node>() {
            @Override
            public Node call(Integer integer) {
                return tableView;
            }
        });
        page.setPageCount(4);
        page.setCurrentPageIndex(0);
        page.currentPageIndexProperty().addListener((observableValue, number, t1) -> {
            bookkeepingListModel.getItems().remove(0);
            tableView.refresh();
        });

        tableView.setItems(bookkeepingListModel.getItems());

        TableColumn<BookkeepingItem, String> tbName = new TableColumn<>("姓名");
        tbName.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<BookkeepingItem, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<BookkeepingItem, String> param) {
                SimpleStringProperty name = new SimpleStringProperty(param.getValue().getName());
                return name;
            }
        });
        tableView.getColumns().add(tbName);

        TableColumn<BookkeepingItem, String> tbMoney = new TableColumn<>("礼金");
        tbMoney.setCellValueFactory(new PropertyValueFactory<BookkeepingItem, String>("money"));
        tableView.getColumns().add(tbMoney);

        TableColumn<BookkeepingItem, String> tbOption = new TableColumn<>("操作");
        tbOption.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<BookkeepingItem, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<BookkeepingItem, String> params) {
                SimpleStringProperty id = new SimpleStringProperty(params.getValue().getId());
                return id;
            }
        });

        tbOption.setCellFactory(new Callback<TableColumn<BookkeepingItem, String>, TableCell<BookkeepingItem, String>>() {
            @Override
            public TableCell<BookkeepingItem, String> call(TableColumn<BookkeepingItem, String> bookkeepingItemStringTableColumn) {
                return new OptionTableListCell();
            }
        });

        tableView.getColumns().add(tbOption);

    }
}
